package com.sduept.nwld.dataserver.manager.distribute;

import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.model.distribute.PersonAndEquipmentDistribute;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonAndEquipmentDistributeManger {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private PersonnelManager pm;
	
	public PersonAndEquipmentDistribute createOrUpdate(PersonAndEquipmentDistribute distribute) {
		return dao.createOrUpdate(distribute, PersonAndEquipmentDistribute.class);
	}
	
	public void delete(PersonAndEquipmentDistribute distribute) {
		dao.delete(distribute.getId(), PersonAndEquipmentDistribute.class);
	}
	
	public List<PersonAndEquipmentDistribute> getEquipmentDistributeByPersonId(String personId) {
		return findEquipmentDistributeByConditions(null,personId,null,null);
	}
	
	public List<PersonAndEquipmentDistribute> getEquipmentDistributeByStationId(String stationId) {
		return findEquipmentDistributeByConditions(null,null,stationId,null);
	}
	
	public List<PersonAndEquipmentDistribute> getAllEquipmentDistribute() {
		return findEquipmentDistributeByConditions(null,null,null,null);
	}
	
	/**
	 * 通过变电站的id查询管理者
	 * @param stationId
	 * @return
	 */
	public List<PersonnelEntityBean> getGovernorBySubstationId(String subStationId) {
		List<PersonAndEquipmentDistribute> distributeList = findEquipmentDistributeByConditions(null,null,subStationId,null);
		List<PersonnelEntityBean> persons = new ArrayList<PersonnelEntityBean>();
		if(distributeList.size()>0) {
			for(PersonAndEquipmentDistribute distribute : distributeList) {
				String personId = distribute.getPersonId();
				if(StringUtils.isNotEmpty(personId)) {
					PersonnelEntityBean person = pm.findPersonnelById(personId);
					if(null != person) {
						persons.add(person);
					}
				}
			}
		}
		return persons;
	}
	
	/**
	 * 按条件查找
	 */
	private List<PersonAndEquipmentDistribute> findEquipmentDistributeByConditions(String id,String personId,String equipmentId,String equipmentName) {
		String sql = " 1=1 ";
		List<Object> values = new ArrayList<>();
		if (StringUtils.isNotEmpty(id)) {
			sql += "and id = ?";
			values.add(id);
		}
		if (StringUtils.isNotEmpty(personId)) {
			sql += "and personId = ?";
			values.add(personId);
		}
		if (StringUtils.isNotEmpty(equipmentId)) {
			sql += "and equipmentId = ?";
			values.add(equipmentId);
		}
		if (StringUtils.isNotEmpty(equipmentName)) {
			sql += "and equipmentName = ?";
			values.add(equipmentName);
		}
		List<PersonAndEquipmentDistribute> distributes = dao.findByCondition(PersonAndEquipmentDistribute.class, sql, values.toArray());
		return distributes;
	}
	
	public List<Substation> findSubstation(String subControlAreaId, String vol) {
		String sql = "";
		List<Object> params = new ArrayList<>();
		if(StringUtils.isNotEmpty(vol)) {
			sql += " and standVoltagelevel = ? ";
			params.add(Float.parseFloat(vol));
		}
		if(StringUtils.isNotEmpty(subControlAreaId)) {
			sql += " and subControlArea = ?";
			params.add(subControlAreaId);
		}
		sql = sql.replaceFirst(" and", "");
		return dao.findByCondition(Substation.class, sql, params.toArray());
	}
	
	public List<String> findSubstation(String subControlAreaId, String vol, String status, String stationName, String hasGov) {
		String sql = "select A.id from t_cim_substation A left join t_person_equipment_distribute B on A.id = B.equipmentId where ";
		if(StringUtils.isNotEmpty(hasGov)) {
			// 是否已有管理者，是：1，否：0
			if("0".equals(hasGov)) {
				sql += " and B.id is null";
			} else if ("1".equals(hasGov)) {
				sql += " and B.id is not null";
			}
		}
		if(StringUtils.isNotEmpty(vol)) {
			sql += " and A.standVoltagelevel = " + vol;
		}
		if(StringUtils.isNotEmpty(subControlAreaId)) {
			sql += " and A.subControlArea = "+subControlAreaId;
		}
		if(StringUtils.isNotEmpty(status)) {
			sql += " and A.status = "+status;
		}
		if(StringUtils.isNotEmpty(stationName)) {
			sql += " and A.name = '"+stationName+"'";
		}
		sql = sql.replaceFirst(" and", "");
		List<String> list = (List<String>) dao.nativeQuery(sql);
		return list;
	}
	
	public List<PersonAndEquipmentDistribute> findEquipmentDistribute(String vol, String status, String stationName, PersonnelEntityBean user) {
		if(null != user) {
			String sql = "select * from t_person_equipment_distribute A left join t_cim_substation B on A.equipmentId = B.id where A.personId = ?";
			List<Object> params = new ArrayList<>();
			params.add(user.getId());
			/*if(!"admin".equals(user.getCode())) {
				sql += " and A.personId = ?";
				params.add(user.getId());
			}*/
			if(StringUtils.isNotEmpty(stationName)) {
				sql += " and A.equipmentName = ? ";
				params.add(stationName);
			}
			if(StringUtils.isNotEmpty(vol)) {
				sql += " and B.standVoltagelevel = ? ";
				params.add(Float.parseFloat(vol));
			}
			if(StringUtils.isNotEmpty(status)) {
				sql += " and B.status = ?";
				params.add(Integer.valueOf(status));
			}
			/*if(params.size() == 0) {
				sql = sql.replaceFirst("where", "");
			}
			sql = sql.replaceFirst(" and", "");*/
			List<PersonAndEquipmentDistribute> list = dao.nativeQuery(PersonAndEquipmentDistribute.class, sql, params.toArray());
			return list;
		} else {
			return null;
		}
	}

}
